package com.btpn.custody.bean;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.btpn.custody.common.Constant;
import com.btpn.custody.dao.IAddress;
import com.btpn.custody.dao.IMasterData;
import com.btpn.custody.entity.Address;
import com.btpn.custody.entity.CIF;
import com.btpn.custody.entity.MasterData;
import com.btpn.custody.service.IMasterService;

/* 
 * Purpose to validate based on CUSTOMER_TYPE :
 * - individual 	: B1,UM,04,05,06
 * - non-individual : B2,NI,03,BI,02
 * 
 * next : pindah ValidationProduct to MasterServiceImpl
 */
@Component("validationProduct")
public class ValidationProduct {
	
	@Autowired
	private IAddress addressDaoHibernate;
	
	@Autowired
	private IMasterData masterDataDaoHibernate;
	
	@Autowired
	private IMasterService masterDataService;
		
	private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
	
	public void validateProducts(String[] mdSids) {
		
		for (String mdSid : mdSids) {	

			// get MasterData
			validateProduct(masterDataDaoHibernate.get(mdSid));			
		}		
	}

	/* 
	 * SPECIFIC validation
	 * Optional validation : be commented
	 * Except : CIF
	 */
	public String validateProduct(MasterData md) {

		// init sb for error message
		StringBuffer sb = new StringBuffer();
				
		// get CIF
		CIF cif = md.getCIF();
		
/* ================================================ Individual ================================================= */
		if (StringUtils.contains(Constant.CST_INDIVIDU, cif.getCUSTOMER_TYPE())) {
			if (StringUtils.isBlank(cif.getCUST_FULL_NAME()))			{sb.append("Nama Lengkap*");}
			//Parent country
			if (StringUtils.isBlank(cif.getGENDER()))					{sb.append("Jenis Kelamin*");}
			if (StringUtils.isBlank(cif.getID_TYPE()))					{sb.append("Kode Identitas*");}
			if (StringUtils.isBlank(cif.getID_NUMBER()))				{sb.append("Nomor Identitas*");}
			if (cif.getID_EXPIRED() == null)							{sb.append("Tanggal Berakhir Kartu Identitas*");}
			else if (cif.getID_EXPIRED().before(new Date())) 			{sb.append("Tanggal Berakhir Kartu Identitas Telah Expired*");}
			if (cif.getBIRTH_DATE() == null)							{sb.append("Tanggal Lahir*");}
			//Tempat lahir
			//NPWP
			if (StringUtils.isBlank(cif.getRELIGION()))					{sb.append("Agama*");}
			//Pendidikan
			if (StringUtils.isBlank(cif.getCURRENT_INCOME()))			{sb.append("Penghasilan*");}
			if (StringUtils.isBlank(cif.getSOURCE_INCOME()))			{sb.append("Sumber Pendapatan*");}
			//Penghasilan Tambahan
			if (StringUtils.isBlank(cif.getPURPOSE()))					{sb.append("Tujuan Penggunaan*");}
			if (StringUtils.isBlank(cif.getPROFESSION()))				{sb.append("Pekerjaan*");}
			if (StringUtils.isBlank(cif.getJABATAN()))					{sb.append("Jabatan*");}
			//Jenis Usaha Tempat Bekerja
			if (StringUtils.isBlank(cif.getMARITAL_STATUS()))			{sb.append("Marital Status*");}
			if (StringUtils.isBlank(cif.getMOTHER_NAME()))				{sb.append("Nama Ibu Kandung*");}
			if (StringUtils.isBlank(cif.getLBU_GOL_NASABAH()))			{sb.append("Golongan Nasabah*");}
			//Saldo Normal
			if (StringUtils.isBlank(cif.getFREQ_TRX_DB()))				{sb.append("Frekuensi Transaksi Debet*");}			
			if (StringUtils.isBlank(cif.getFREQ_TRX_CR()))				{sb.append("Frekuensi Transaksi Kredit*");}			
			if (StringUtils.isBlank(cif.getEST_TRX_NORMAL_DB()))		{sb.append("Average Transaksi Debet*");}
			if (StringUtils.isBlank(cif.getEST_TRX_NORMAL_CR()))		{sb.append("Average Transaksi Kredit*");}
			//Nama Ahli Waris
			//Alamat Ahli Waris 1
			//Alamat Ahli Waris 2
			//Kota Ahli Waris
			//Hubungan Ahli Waris
			//Rekening di Bank Lain 1
			//Rekening di Bank Lain 2
			//Rekening di Bank Lain 3
			//High Risk Customer
			//High Risk Business
			//High Risk Countries
			//High Risk Product			

/* ================================================ Non-Individual ================================================= */	
		} else if (StringUtils.contains(Constant.CST_NON_INDIVIDU, cif.getCUSTOMER_TYPE())) {
			
			if (StringUtils.isBlank(cif.getCUST_FULL_NAME()))			{sb.append("Nama Lengkap*");}
			//Parent country
			if (StringUtils.isBlank(cif.getINFO_AKTE_PENDIRIAN()))		{sb.append("Akte Pendirian/AD TGL 1*");}
			if (StringUtils.isBlank(cif.getINFO_AKTE_PENDIRIAN2()))		{sb.append("Akte Pendirian/AD TGL 2*");}
			if (StringUtils.isBlank(cif.getAKTE_TERAKHIR_TANGGAL1()))	{sb.append("Akte Terakhir & Tanggal 1*");}
			if (StringUtils.isBlank(cif.getAKTE_TERAKHIR_TANGGAL2()))	{sb.append("Akte Terakhir & Tanggal 2*");}
			if (StringUtils.isBlank(cif.getSIUP()))						{sb.append("Surat Ijin Usaha dari Inst. Berwenang*");}
			//Tanda Daftar Perusahaan
			if (cif.getTGL_AKHIR_TDP() == null)							{sb.append("Tanggal Berakhir TDP*");}
			//NPWP
			if (StringUtils.isBlank(cif.getCURRENT_INCOME()))			{sb.append("Penghasilan*");}
			if (StringUtils.isBlank(cif.getSOURCE_INCOME()))			{sb.append("Sumber Pendapatan*");}
			//Penghasilan Tambahan
			if (StringUtils.isBlank(cif.getPURPOSE()))					{sb.append("Tujuan Penggunaan*");}
			//Jenis Usaha Tempat Bekerja
			if (StringUtils.isBlank(cif.getLBU_GOL_NASABAH()))			{sb.append("Golongan Nasabah*");}
			if (StringUtils.isBlank(cif.getFREQ_TRX_DB()))				{sb.append("Frekuensi Transaksi Debet*");}
			if (StringUtils.isBlank(cif.getFREQ_TRX_CR()))				{sb.append("Frekuensi Transaksi Kredit*");}
			if (StringUtils.isBlank(cif.getEST_TRX_NORMAL_DB()))		{sb.append("Average Transaksi Debet*");}
			if (StringUtils.isBlank(cif.getEST_TRX_NORMAL_CR()))		{sb.append("Average Transaksi Kredit*");}
			//Rekening di Bank Lain 1
			//Rekening di Bank Lain 2
			//Rekening di Bank Lain 3
			//High Risk Customer
			//High Risk Business
			//High Risk Countries
			//High Risk Product
			if (StringUtils.isBlank(cif.getLAP_KEUANGAN()))				{sb.append("Laporan Keuangan*");}
			if (StringUtils.isBlank(cif.getHUB_DENGAN_BANK()))			{sb.append("Hubungan Dengan Bank*");}
			
		} else {
			logger.warn("UNMATCH Customer Type at Validation : CIF[" + md.getCIF().getCifSid() + "] CustType[" + md.getCIF().getCUSTOMER_TYPE() + "]");
		}

		// prime address validation
		Address primeAddress = addressDaoHibernate.getByMD_AddressType(md.getSid(), Address.PRIME_ADDRESS);
		if (primeAddress!=null) {
			if (StringUtils.isBlank(primeAddress.getADDRESS1())) {sb.append("Prime Address*");}
		}

		return masterDataService.decideValidated(md, sb.toString());			
	}
}